package com.example.human_resource_management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.human_resource_management.common.ResponseData;
import com.example.human_resource_management.config.UserContextUtils;
import com.example.human_resource_management.entry.dto.AttendanceEmployeeDto;
import com.example.human_resource_management.entry.po.Attendance;
import com.example.human_resource_management.service.AttendanceService;
import com.example.human_resource_management.mapper.AttendanceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【attendance(考勤记录表)】的数据库操作Service实现
* @createDate 2025-06-09 11:48:49
*/
@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance>
    implements AttendanceService{
    @Autowired
    private AttendanceMapper attendanceMapper;

    @Override
    public List<Attendance> getAttenById(Long empId) {
        // 使用条件查询，查询该员工的所有打卡记录
        QueryWrapper<Attendance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("emp_id", empId);
        return attendanceMapper.selectList(queryWrapper);
    }

    private static final int NORMAL_START_HOUR = 9;

    @Override
    public ResponseData<String> addAttendanceRecord(LocalDateTime punchTime) {
        Long empId = UserContextUtils.getEmpId();
        if (empId == null) {
            return ResponseData.warn("未获取到员工信息");
        }
        Attendance record = new Attendance();
        record.setEmpId(empId);
        record.setPunchTime(punchTime);
        record.setAttendDate(punchTime.toLocalDate()); // 自动设置当天日期

        record.setStatus(isLate(punchTime) ? "迟到" : "正常");
        return save(record) ?
                ResponseData.ok("打卡成功") :
                ResponseData.warn("打卡失败");
    }
    private boolean isLate(LocalDateTime punchTime) {
        return punchTime.toLocalTime().isAfter(
                LocalTime.of(NORMAL_START_HOUR, 0)
        );
    }





//管理员端
@Override
public List<AttendanceEmployeeDto> selectAttendanceEmployeeList() {
    return attendanceMapper.selectAttendanceEmployeeList();
}

    @Override
    public AttendanceEmployeeDto selectAttendanceEmployeeById(Long recordId) {
        return attendanceMapper.selectAttendanceEmployeeById(recordId);
    }

    @Override
    public List<AttendanceEmployeeDto> selectAttendanceByDate(LocalDate date) {
        return attendanceMapper.selectAttendanceByDate(date);
    }

    @Override
    public boolean addAttendance(Attendance attendance) {
        // 检查员工ID是否存在
        // 这里可以添加业务逻辑验证

        return save(attendance);
    }

    @Override
    public boolean updateAttendance(Attendance attendance) {
        return updateById(attendance);
    }

    @Transactional
    @Override
    public boolean deleteAttendance(Long recordId) {
        return removeById(recordId);
    }
}




